package com.dxf.sysadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxf.common.utils.ResultJson;
import com.dxf.sysadmin.entity.Clazz;
import com.dxf.sysadmin.entity.Dept;
import com.dxf.sysadmin.entity.qo.MyPage;
import com.dxf.sysadmin.mapper.DeptMapper;
import com.dxf.sysadmin.service.DeptService;
import com.dxf.sysadmin.utils.DeptTree;
import com.dxf.sysadmin.utils.TreeHpler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门表 服务实现类
 */
@Slf4j
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    /**
     *部门列表treetable
     */
    @Override
    public ResultJson deptPage() {
        List<DeptTree> deptTree = TreeHpler.getDeptTree(this);
        return ResultJson.ok().data("deptTree",deptTree);
    }

    /**
     * 更具id列表删除部门
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson deleteDept(Integer id) {
        removeById(id);
        remove(new QueryWrapper<Dept>().lambda().eq(Dept::getParentId,id));
        return ResultJson.ok();
    }

    /**
     *新增或者更新部门
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultJson saveOrUpdateDept(Dept dept) {
        if (dept.getId()==null && dept.getParentId()==null){
            dept.setParentId((long) 0);
        }
        saveOrUpdate(dept);
        return ResultJson.ok();
    }

    /**
     *部门添加时父级选择的SelectTree数据
     */
    @Override
    public ResultJson selectTree() {
        List<Dept> list = list(new QueryWrapper<Dept>().lambda().eq(Dept::getParentId,0));
        return ResultJson.ok().data("selectTree",list);
    }
}
